using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.StreetEvent;

namespace com.yoozoo.gta.Art.RpgScene
{
    public class RpgNightSettings : MonoBehaviour
    {
        [Header("每帧更新(Debug)")]
        public bool preUpdate = false;

        public bool isFog = false;

        [Header("开启点位2")]
        public bool isFog2 = false;
        //[Header("开启雾效")]
        //private bool fogEnable = false;
        [Header("颜色")][SerializeField]
        private Color fogColor = Color.white;
        [Header("整体密度")][SerializeField]
        [Range(0.0f,1.0f)]private float fogDensity = 1.0f;
        [Header("开始距离")][SerializeField]
        private float fogStart = 1.0f;
        [Header("结束距离")][SerializeField]
        private float fogEnd = 550.0f;
        [Header("距离指数")][SerializeField]
        [Range(0.0f,10.0f)]private float fogDistanceExp = 1.0f;
        [Header("高度指数")][SerializeField]
        [Range(0.0f,10.0f)]private float fogHeightExp = 1.0f;

        [Header("________________________________________________________________________________________________________________________________________________________")]
        [Header("Second fog settings")]
        [Header("开始距离")][SerializeField]
        private float secondFogStart = 1.0f;
        [Header("结束距离")][SerializeField]
        private float secondFogEnd = 550.0f;
        [Header("距离指数")][SerializeField]
        [Range(0.0f,10.0f)]private float secondFogDistanceExp = 1.0f;
        [Header("高度指数")][SerializeField]
        [Range(0.0f,10.0f)]private float secondFogHeightExp = 1.0f;

        [Header("________________________________________________________________________________________________________________________________________________________")]
        [SerializeField][Header("光照范围")]private float lightScale;
        [SerializeField][Header("平滑度")] private Vector3 gradientSmooth = new Vector3(0, 1, 1);
        [SerializeField][Header("光照颜色")] private Color lightColor = Color.white;

        private static readonly int m_setSpoltScale = Shader.PropertyToID("_SetSpoltScale");
        private static readonly int m_setSpoltGradient = Shader.PropertyToID("_SetSpoltGradient");
        private static readonly int m_setSpoltColor = Shader.PropertyToID("_SetSpoltColor");

        private bool m_isFog;

        private void Start()
        {
            RpgNightSettingExtension.Init(this.GetComponent<RpgNightSettings>());
        }

        void Update()
        {
            //#if UNITY_EDITOR

            //编辑器下勾选每帧更新后强制开启RPG夜晚模式
            if (preUpdate)
            {
                RpgNightSettingExtension.NightMode(true, isFog2 ? 2 : 1);
            }

            if (m_isFog != isFog)
            {
                RpgNightSettingExtension.NightMode(isFog, isFog2 ? 2 : 1);
                m_isFog = isFog;
            }
            SetSpoltLight();

            //#endif
        }

        void SetSpoltLight()
        {
            Shader.SetGlobalFloat(m_setSpoltScale, lightScale);
            Shader.SetGlobalVector(m_setSpoltGradient,gradientSmooth);
            Shader.SetGlobalColor(m_setSpoltColor, lightColor);
        }

        public static class RpgNightSettingExtension
        {
            private static RpgNightSettings _instance;
            private static bool _isFog;

            private static readonly int m_setFogOpen = Shader.PropertyToID("_LowFogOpen");
            private static readonly int m_setFogColor = Shader.PropertyToID("_FogColor");
            private static readonly int m_setFogSetting = Shader.PropertyToID("_FogSetting");
            private static readonly int m_setSecondFogSetting = Shader.PropertyToID("_SecondFogSetting");

            public static void Init(RpgNightSettings instance)
            {
                _instance = instance;
                _instance.isFog = _isFog;
                NightMode(_instance.isFog);
            }

            /// <summary>
            /// 启用Rpg夜晚模式,且雾效只围绕一个点位
            /// </summary>
            /// <param name="fogState">是否开启夜晚模式</param>
            public static void NightMode(bool fogState)
            {
                _isFog = fogState;
                if (_instance == null)
                {
                    return;
                }
                //BattleDebug.LogError("启用Rpg夜晚模式" + fogState);
                Shader.SetGlobalInt(m_setFogOpen,fogState?1:0);
                if (fogState)
                {
                    Shader.EnableKeyword("_RPG_NIGHT_ON");
                    _instance.SetSpoltLight();
                    Shader.SetGlobalColor(m_setFogColor, new Color(_instance.fogColor.r,_instance.fogColor.g,_instance.fogColor.b,_instance.fogDensity));
                    Shader.SetGlobalVector(m_setFogSetting,new Vector4(_instance.fogStart,_instance.fogEnd,_instance.fogDistanceExp,_instance.fogHeightExp));
                    Shader.SetGlobalVector(m_setSecondFogSetting,new Vector4(_instance.secondFogStart,_instance.secondFogEnd,_instance.secondFogDistanceExp,_instance.secondFogHeightExp));
                }
                else
                {
                    Shader.DisableKeyword("_RPG_NIGHT_ON");
                }
            }

            /// <summary>
            /// 启用Rpg夜晚模式,且雾效围绕2个点位
            /// </summary>
            /// <param name="fogState">是否开启夜晚模式</param>
            /// <param name="pointNum"> "=1"：只开启第一个点位。"=2"：同时开启两个点位</param>
            public static void NightMode(bool fogState, int pointNum)
            {
                _isFog = fogState;
                if (_instance == null)
                {
                    return;
                }
                //BattleDebug.LogError("启用Rpg夜晚模式" + fogState);
                Shader.SetGlobalInt(m_setFogOpen,fogState?1:0);
                if (fogState)
                {
                    if (pointNum == 1)
                    {
                        Shader.DisableKeyword("_RPG_NIGHT_TWOPOINT_ON");
                        Shader.EnableKeyword("_RPG_NIGHT_ON");
                    }
                    else if(pointNum == 2)
                    {
                        Shader.DisableKeyword("_RPG_NIGHT_ON");
                        Shader.EnableKeyword("_RPG_NIGHT_TWOPOINT_ON");
                    }
                    _instance.SetSpoltLight();
                    Shader.SetGlobalColor(m_setFogColor, new Color(_instance.fogColor.r,_instance.fogColor.g,_instance.fogColor.b,_instance.fogDensity));
                    Shader.SetGlobalVector(m_setFogSetting,new Vector4(_instance.fogStart,_instance.fogEnd,_instance.fogDistanceExp,_instance.fogHeightExp));
                    Shader.SetGlobalVector(m_setSecondFogSetting,new Vector4(_instance.secondFogStart,_instance.secondFogEnd,_instance.secondFogDistanceExp,_instance.secondFogHeightExp));
                }
                else
                {
                    Shader.DisableKeyword("_RPG_NIGHT_ON");
                    Shader.DisableKeyword("_RPG_NIGHT_TWOPOINT_ON");
                }
            }
        }
        
    }
    
    
}
